package com.sjc.designpattern.factory;

/**
 * @Classname AbstractFactoryPatternDemo
 * @Description 抽象工厂方法
 * @Date 2020/4/25 10:34
 * @Created by SangJiacun
 */
public class AbstractFactoryPatternDemo {
    public static void main(String[] args) {
        // 产品A1+产品B1 -> 产品A1+产品B3
        ProductA productA1 = Factory1.get().createProductA();
        ProductB productB1 = Factory1.get().createProductB();
        productA1.execute();
        productB1.execute();

        // 产品A2+产品B2
        ProductA productA2 = Factory2.get().createProductA();
        ProductB productB2 = Factory2.get().createProductB();
        productA2.execute();
        productB2.execute();

        // 产品A3+产品B3
        ProductA productA3 = Factory3.get().createProductA();
        ProductB productB3 = Factory3.get().createProductB();
        productA3.execute();
        productB3.execute();

        // 哪怕你有100个地方定义了产品组合，要调整组合的逻辑，只要修改一个工厂就可以了
    }

    public interface ProductA {
        void execute();
    }

    public static class ProductA1 implements ProductA {
        @Override
        public void execute() {
            System.out.println("产品A1的功能逻辑");
        }
    }

    public static class ProductA2 implements ProductA {
        @Override
        public void execute() {
            System.out.println("产品A2的功能逻辑");
        }
    }

    public static class ProductA3 implements ProductA {
        @Override
        public void execute() {
            System.out.println("产品A3的功能逻辑");
        }
    }

    public interface ProductB {
        void execute();
    }

    public static class ProductB1 implements ProductB {
        @Override
        public void execute() {
            System.out.println("产品B1的功能逻辑");
        }
    }

    public static class ProductB2 implements ProductB {
        @Override
        public void execute() {
            System.out.println("产品B2的功能逻辑");
        }
    }

    public static class ProductB3 implements ProductB {
        @Override
        public void execute() {
            System.out.println("产品B3的功能逻辑");
        }
    }

    public interface Factory {
        ProductA createProductA();
        ProductB createProductB();
    }

    public static class Factory1 implements Factory {

        private static final Factory1 instance = new Factory1();

        private Factory1() {
        }

        public static Factory get() {
            return instance;
        }

        @Override
        public ProductA createProductA() {
            return new ProductA1();
        }

        @Override
        public ProductB createProductB() {
            return new ProductB3();
        }
    }

    public static class Factory2 implements Factory {

        private static final Factory2 instance = new Factory2();

        private Factory2() {
        }

        public static Factory get() {
            return instance;
        }

        @Override
        public ProductA createProductA() {
            return new ProductA2();
        }

        @Override
        public ProductB createProductB() {
            return new ProductB2();
        }
    }

    public static class Factory3 implements Factory {

        private static final Factory3 instance = new Factory3();

        private Factory3() {
        }

        public static Factory get() {
            return instance;
        }

        @Override
        public ProductA createProductA() {
            return new ProductA3();
        }

        @Override
        public ProductB createProductB() {
            return new ProductB3();
        }

    }
}
